<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\User;
use Api;
use Mail;
class UserController extends Controller
{
    //
    public function register(Request $req)
    {
        //{nickname,email,password}
        //Api::setDebug(true);
        if(User::where('email',$req->input('email'))->first() === null)
        {
            $user = new User;
            $user->nickname = $req->input('nickname');
            $user->password = $req->input('password');
            $user->email = $req->input('email');
            $user->active = 0;//not active
            $user->state = 0;//require email valid
            $user->permission = 0;//normal permission
            $user->save();
            $this->sendValidateEmail($user);

        }
        else
        {
            Api::setState(2);
            Api::addError(6);
        }

        return null;
    }
    
    public function resendValidateEmail(Request $req)
    {
        $user = User::where('email',$req->input('email'))->first();
        if($user->state === 0 )
        {
            $this->sendValidateEmail($user);  
        }
        else 
        {
            Api::setState(2);
            Api::addError(5);
        }
        return null;
    }
    
    public function resendValidateEmailWeb(Request $req)//web
    {
        $user = User::where('email',$req->input('e'))->first();
        if($user->state === 0 )
        {
            $this->sendValidateEmail($user);  
        }
        else 
        {
            return '<h1>您的账户已完成验证</h1>';
        }
        return '<h1>一封新的认证邮件已发出</h1>';
    }
    
    function  sendValidateEmail($user)
    {
            $token = $user->email_token = Api::generateToken($user->nickname.$user->email);
            $user->email_token_created_at = Api::getCurrentTimestamp();
            $user->save();
            $validUrl = url('/user/emailValidate?'.'e='.urlencode($user->email).'&t='.urlencode($token));
            Mail::send('emailValidate', ['url' => $validUrl], function ($m) use ($user) {
                $m->from('admin@laxalex.net', 'DPS');

                $m->to($user->email, $user->nickname)->subject('用户注册确认邮件');
            });
    }

    
    
        public function emailValidate(Request $req)//web
    {
        $user = User::where('email',$req->input('e'))->first();
        if($user === null || $user->email_token != $req->input('t') )
        {
            return '<h1>认证失败,您已完成注册或请您重新注册</h1>';
        }
        
        if(Api::isValidateEmailExpired($user))
        {
            return '<h1>您的验证邮件已过期，您可以<a href="' . url('/user/resendValidateEmail?' . 'e=' . urlencode($user->email)) . '">---点我重新发送一封新的验证邮件---</a></h1>';
        }
        
        $user->email_token = null;
        $user->email_token_created_at = null;
        $user->state = 1;
        $user->save();
        
        return '<h1>感谢您的注册</h1>';
        //return ;
    }
    
    public function login(Request $req)
    {
        //{email,password} {api_token}
        //Api::setDebug(true);
        $user = User::where('email',$req->input('email'))->first();
        if($user === null)
        {
            Api::setState(2);
            Api::addError(1);
            return null;
        }
        if($user->state != 1)
        {
            Api::setState(2);
            if($user->state === 0)
            {
                Api::addError(7);
            }
            Api::addError(3);
            return null;
        }
        if($user->password !== $req->input('password'))
        {
            Api::setState(2);
            Api::addError(8);
            return null;
        }
        $user->api_token = Api::generateToken($user->nicakname,$user->created_at);
        $user->api_token_created_at = Api::getCurrentTimestamp();
        $user->heartbeat_at = Api::getCurrentTimestamp();
        $user->save();
        return ['api_token'=>$user->api_token];
    }
    
    public function resetPassword(Request $req)
    {
        //{email,password}
        $user = User::where('email',$req->input('email'))->first();
        if($user === null)
        {
            Api::setState(2);
            Api::addError(1);
            return null;
        }
        if($user->state != 1)
        {
            Api::setState(2);
            Api::addError(3);
            return null;
        }
        $token = $user->email_token = Api::generateToken($req->input('nickname').$req->input('email'));
        $user->email_token_created_at = Api::getCurrentTimestamp();
        $user->save();
        $validUrl = url('/user/resetPasswordValidate?'.'e='.urlencode($req->input('email')).'&t='.urlencode($token).'&p='.$req->input('password'));
        Mail::send('resetPasswordValidate', ['url' => $validUrl], function ($m) use ($user) {
                $m->from('admin@laxalex.net', 'DPS');

                $m->to($user->email, $user->nickname)->subject('修改密码确认邮件');
            });
            return null;
    }
    
    public function resetPasswordValidate(Request $req)//web
    {
        //{email e,email_token t,password p}
        $user = User::where('email',$req->input('e'))->first();
        if($user === null || $user->email_token != $req->input('t') )
        {
            return '<h1>重设密码失败，您之前已完成重设或重新尝试</h1>';
        }
        
        $user->email_token = null;
        $user->state = 1;
        $user->password = $req->input('p');
        $user->save();
        
        return '<h1>密码重设成功，感谢您的使用</h1>';
    }
    
    public function heartbeat(Request $req)
    {

        $user = User::where('email',$req->input('email'))->first();
        
        $user->heartbeat_at = Api::getCurrentTimestamp();
        $user->save();
        return null;
    }
}
